import {
    delDeviceCheckItemDict,
    deviceCheckItemDictPage,
    genCheckItemCode,
    saveDeviceCheckItemDict
} from "@/api/dev/deviceCheckItemDictApi";
import {getLoading, getLoadingObj} from "@/utils";

let query = {
    page: 1,
    limit: 10,
    name: null,
    code: null,
    type:1,
}

let saveData = {
    code: null,
    name: null,
    content: null,
    type:1,
}

export default {
    name: "deviceCheckItemDict",
    props: {isChoose: {required: false, default: false, type: Boolean}, selectChangFunc: {required: false, type: Function}},
    data() {
        return {
            showSearch: true,
            loading: false,
            total: .0,
            list: [],
            query: {...query},
            createFlag: false,
            saveData: {...saveData},
            formRule: {
                code: [{required: true, message: "项目编码不能为空", trigger: "change"}],
                name: [{required: true, message: "项目名称不能为空", trigger: "change"}],
                content: [{required: true, message: "项目内容不能为空", trigger: "change"}]
            },
          saveLoading: false,
        }
    },
    created() {
        this.getList()
    },
    methods: {
        delFunc(row) {
            this.$confirm("此操作将会永久删除该数据，是否继续？", "提示", {
                confirmButtonText: "继续",
                cancelButtonText: "取消"
            }).then(() => {
                let load = getLoading(this)
                delDeviceCheckItemDict(row.code).then(res => {
                    load.close()
                    if (res.code != 20000) {
                        this.$message.error(res.message)
                        return
                    }
                    this.$message.success(res.message)
                    this.query = {...query}
                    this.getList()
                }).catch(() => {
                    load.close()
                })
            })
        },
        genCode() {
            genCheckItemCode().then(res => {
                if (res.code != 20000) {
                    this.$message.error(res.message)
                    return
                }
                this.saveData.code = res.data
            })
        },
        saveFunc: function () {
            this.$refs.saveForm.validate(b => {
                if (!b) {
                    return false
                }
                let loading = getLoading(this);
                  this.saveLoading = true
                saveDeviceCheckItemDict(this.saveData).then(res => {
                  this.saveLoading = false
                    loading.close()
                    if (res.code != 20000) {
                        this.$message.error(res.message)
                        return
                    }
                    this.$message.success(res.message)
                    this.createFlag = false
                    this.query.page = 1
                    this.getList()
                }).catch(() => {
                    this.saveLoading = false
                    loading.close
                })
            })
        },
        openCreate() {
            genCheckItemCode().then(res => {
                if (res.code != 20000) {
                    this.$message.error(res.message)
                    return
                }
                this.saveData = {...saveData, code: res.data}
                this.createFlag = true
            })
        },
        search() {
            this.query.page = 1
            this.getList()
        },
        onReset() {
            this.query = {...query}
            this.getList()
        },
        getList() {
            this.loading = true
            deviceCheckItemDictPage(this.query).then(res => {
                this.loading = false
                if (res.code != 20000) {
                    this.$message.error(res.message)
                    return
                }
                this.list = res.data.list || []
                this.total = res.data.total || 0

            }).catch(e => {
                this.loading = false
            })
        }
    }
}
