import {
    observable,
    action,
    computed, extendObservable
} from 'mobx';
import * as mobx from 'mobx';
import * as API from '../apis/qtxConfig';
import {
    WeaForm
} from 'comsMobx';
import {
    Modal,
    message,
} from 'antd'
import {
    i18n
} from '../public/i18n';
import { WeaLocaleProvider } from 'ecCom';

const toJS = mobx.toJS;
const getLabel = WeaLocaleProvider.getLabel;


export class QtxConfigStore {

    @observable configForm = new WeaForm();
    @observable editTableData = {
        datas: [],
        columns: [],
        loading: true,
        selectedData: {}
      };

    @observable configFields = [
        {
            domkey: ['isopenconfig'],
            conditionType: 'SWITCH',
            label: getLabel('547394', "是否开启数据库配置"),
            labelcol: 5,
            fieldcol: 19,
            viewAttr: 3,
        }
    ]

    @action("初始化页面") initList = () => {
        this.configForm.initFormFields([{ items: this.configFields.slice() }]);
        this.getQtxConfigInfo();
    }

    @action clearConfigData = () => {
        this.editTableData.datas = [];
        this.editTableData.columns = [];
        this.editTableData.loading = true;
       
    }

    @action("数据获取") getQtxConfigInfo = () => {
        this.clearConfigData();
        API.getQtxConfigInfo().then(action(({ code, data, msg }) => {
            if (code == 200) {
                const { columns, datas } = data;
                extendObservable(this.editTableData, {
                    datas: datas,
                    columns: columns,
                    loading: false
                  });
                this.configForm.updateFields({
                    isopenconfig: data.isopenconfig,
                });
            } else {
                message.error(`[${getLabel(127353, "错误") + code}]:${msg}`);
            }
        }));
    }

    setTableEditDatas = (e) => {
        if(e.length > 1) {
            e.pop();
            message.error(getLabel(547632, "默认只能添加一条配置数据！！！"))
        }
        extendObservable(this.editTableData, {
          datas: e
        });
      }

    @action("保存页面") saveQtxConfigInfo = () => {
        if(this.editTableData.datas.length == 0) {
            return message.error(getLabel(547633, "至少保存一条配置数据！！！"))
        }
        let editTable = JSON.stringify(this.editTableData.datas[0]);
        API.saveQtxConfigInfo({
            ...this.configForm.getFormParams(),
            editTable,
        }).then(({ code, data, msg }) => {
            if (code == 200) {
                message.success(getLabel(83551, '保存成功!'));
                this.getQtxConfigInfo();
            } else {
                message.error(`[${getLabel(127353, "错误") + code}]:${msg}`);
            }
        });
    }

    arrToJson = (arr) => {
        let json = {};
        const _rows = isEmpty(arr) ? [] : rows;
        _rows && _rows.map(index => {
          arr[index][rowKey] = "1";
        });
        arr.map((item, index) => {
          if (!item[rowKey]) item[rowKey] = "0";
          for (let key in item) {
            json[key + "_" + index] = item[key];
          }
        });
    
        return json;
      }
  

    /**===========================buttons=========================== */
    @observable rightMenu = [
        {
            "isBatch": "0",
            "isTop": "1",
            "menuFun": "save",
            "menuIcon": "icon-coms-Preservation",
            "menuName": getLabel('547360', "保存"),
            "type": "BTN_save"
        },
        {
            "isBatch": "0",
            "isTop": "0",
            "menuFun": "login",
            "menuIcon": "icon-coms-link",
            "menuName": getLabel('547396', "单点登录"),
            "type": "BTN_link"
        }
    ];
    @observable topMenu = [
        {
            "isBatch": "1",
            "isTop": "1",
            "menuFun": "save",
            "menuIcon": "icon-coms-Preservation",
            "menuName": getLabel('547360', "保存"),
            "type": "BTN_save"
        },
        {
            "isBatch": "1",
            "isTop": "1",
            "menuFun": "login",
            "menuIcon": "icon-coms-link",
            "menuName": `${i18n.button.ssoLogin()}`,
            "type": "BTN_link"
        }
    ]

}